package com.resou.reader.reader.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.resou.reader.utils.UIHelper;

/**
 * Created by ${Garrett} on 2018/9/13.
 * Contact me krouky@outlook.com
 */
public class SpUtils {

    private static final String THEME = "isNightTheme";
    private static final String BG_INDEX = "bgIndex";
    private static final String SPACE_INDEX = "spaceIndex";
    private static final String SPACE_TIMES = "spaceTimes";
    private static final String TEXT_SIZE = "textSize";
    private static final String ANIM_INDEX = "animation";
    private static final String READ_PROGRESS = "readProgress";
    private static final String PROTECT_EYE = "protectEye";


    // 夜间模式返回true
    public static boolean getTheme(Context context) {
        return getSp(context).getBoolean(THEME, false);
    }

    public static boolean setTheme(Context context, boolean isNight) {
        return getSp(context).edit().putBoolean(THEME, isNight).commit();
    }

    public static boolean getProtectEye(Context context) {
        return getSp(context).getBoolean(PROTECT_EYE, false);
    }

    public static boolean setProtectEye(Context context, boolean isOpen) {
        return getSp(context).edit().putBoolean(PROTECT_EYE, isOpen).commit();
    }

    public static int getBgIndex(Context context) {
        return getSp(context).getInt(BG_INDEX, 0);
    }

    public static boolean setBgIndex(Context context, int bgIndex) {
        return getSp(context).edit().putInt(BG_INDEX, bgIndex).commit();
    }

    public static int getSpaceIndex(Context context) {
        return getSp(context).getInt(SPACE_INDEX, 0);
    }

    public static boolean setSpaceIndex(Context context, int spaceIndex) {
        return getSp(context).edit().putInt(SPACE_INDEX, spaceIndex).commit();
    }

    public static int getSpaceTimes(Context context) {
        return getSp(context).getInt(SPACE_TIMES, 40);
    }

    public static boolean setSpaceTimes(Context context, int spaceIndex) {
        return getSp(context).edit().putInt(SPACE_TIMES, spaceIndex).commit();
    }

    public static int getTextSize(Context context, int defaultSize) {
        return getSp(context).getInt(TEXT_SIZE, defaultSize);
    }

    public static boolean setTextSize(Context context, int textSize) {
        return getSp(context).edit().putInt(TEXT_SIZE, textSize).commit();
    }

    public static int getReadLight(Context context, int defaultSize) {
        return getSp(context).getInt(READ_PROGRESS, defaultSize);
    }


    public static boolean setReadLight(Context context, int progress) {
        return getSp(context).edit().putInt(READ_PROGRESS, progress).commit();
    }

    private static SharedPreferences getSp(Context context) {
        return context.getSharedPreferences("readerTheme", Context.MODE_PRIVATE);
    }

    //第一次打开app书架需要添加W
    public static boolean getWhetherAddShelf(Context context) {
        return context.getSharedPreferences("firstOpen", Context.MODE_PRIVATE).getBoolean("addShelfState", true);
    }

    public static boolean setWhetherAddShelf(Context context) {
        return context.getSharedPreferences("firstOpen", Context.MODE_PRIVATE).edit().putBoolean("addShelfState", false).commit();
    }

    public static boolean saveLoginState(Context context, String token) {
        return getLoginSp(context).edit().putString("token", token).commit();
    }

    private static final String TAG = "SpUtils-App";

    public static boolean clearLoginState(Context context) {
        return getLoginSp(context).edit().clear().commit();
    }

    public static String getLoginState(Context context) {
        return getLoginSp(context).getString("token", "");
    }

    private static SharedPreferences getLoginSp(Context context) {
        return context.getSharedPreferences("Login", Context.MODE_PRIVATE);
    }

    public static int getAnimIndex(Context context) {
        return getSp(context).getInt(ANIM_INDEX, 0);
    }

    public static boolean setAnimIndex(Context context, int index) {
        return getSp(context).edit().putInt(ANIM_INDEX, index).commit();
    }
}
